package com.situ.mvc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mvc.entity.Banji;
import com.situ.mvc.entity.PageBean;
import com.situ.mvc.entity.Student;
import com.situ.mvc.service.IBanjiService;
import com.situ.mvc.service.IStudentService;
import com.situ.mvc.service.impl.BanjiServiceImpl;
import com.situ.mvc.service.impl.StudentServiceImpl;
import com.situ.mvc.vo.StudentSearchCondition;

@Controller
@RequestMapping(value = "/student")
public class StudentController {
	
	/*@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 把字符串的日期类型，转换成Date
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		simpleDateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
	}*/
	
	IStudentService studentService = new StudentServiceImpl();
	IBanjiService banjiService = new BanjiServiceImpl();
	
	@RequestMapping(value = "/pageList")
	public String pageList(Integer pageNo, Integer pageSize, String name, Integer age, String gender, Model model) {
		// 1、接收请求参数
		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 3;
		}
		// 把所有的搜索条件封装成了StudentSearchCondition这样一个对象
		StudentSearchCondition searchCondition = new StudentSearchCondition(pageNo, pageSize, name, age, gender);
		// 2、封装成PageBean，调用Service层业务逻辑
		PageBean pageBean = studentService.getPageBean(searchCondition);
		System.out.println(pageBean);
		model.addAttribute("pageBean", pageBean);
		model.addAttribute("searchCondition", searchCondition);

		return "/student/student_list";
	}
	
	// /student/deleteById.action&id=6
	@RequestMapping(value="/deleteById")
	@ResponseBody
	public Boolean deleteById(Integer id) {
		return studentService.deleteById(id);
		//return "redirect:/student/pageList.action";
	}
	
	
	@RequestMapping(value="/getInsertPage")
	public String getInsertPage(Model model) {
		List<Banji> list = banjiService.list();
		model.addAttribute("list", list);
		return "/student/student_insert";
	}
	
	@RequestMapping(value="/insert")
	@ResponseBody
	public Boolean insert(Student student) {
		System.out.println(student);
		return studentService.insert(student);
		//return "redirect:/student/pageList.action";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public List<Student> list() {
		return studentService.list();
	}
}
